package com.archie.xunji;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.archie.xunji.adapter.TabViewPagerAdapter;
import com.archie.xunji.fabu.FaBuActivity;
import com.archie.xunji.guangchang.fragments.GuangchangFragment;
import com.archie.xunji.guanzhu.AttentionFragment;
import com.archie.xunji.guanzhu.peoples.PeopleActivity;
import com.archie.xunji.huodong.fragments.HuodongFragment;
import com.archie.xunji.map.discover.DiscoverActivity;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, ViewPager.OnPageChangeListener, View.OnTouchListener {

    private View viewTabBiaoJi;
    private RelativeLayout relativeLayout;
    private ViewPager viewPager;
    private DrawerLayout drawerLayout;

    private boolean isExit;
    private LinearLayout drawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        isExit = false;

        initView();

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new GuangchangFragment());
        fragments.add(new HuodongFragment());
        fragments.add(new AttentionFragment());
        TabViewPagerAdapter pagerAdapter = new TabViewPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOnPageChangeListener(this);
    }

    private void initView() {

        ImageView imgBtnFaBu = (ImageView) findViewById(R.id.imgBtn_fubu);
        imgBtnFaBu.setOnClickListener(this);

        relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

        TextView tabGuangChang = (TextView) findViewById(R.id.textView_tab_guangchang);
        tabGuangChang.setOnClickListener(this);

        TextView tabHuoDong = (TextView) findViewById(R.id.textView_tab_huodong);
        tabHuoDong.setOnClickListener(this);

        TextView tabGuanZhu = (TextView) findViewById(R.id.textView_tab_guanzhu);
        tabGuanZhu.setOnClickListener(this);

        viewTabBiaoJi = findViewById(R.id.view_tab_biaoji);

        viewPager = (ViewPager) findViewById(R.id.viewPager);

        ImageView imageViewMine = (ImageView) findViewById(R.id.imageView_mine);
        imageViewMine.setOnClickListener(this);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);

        drawer = (LinearLayout) findViewById(R.id.linearLayout_drawer);
        drawer.setOnTouchListener(this);

        TextView explore = (TextView) findViewById(R.id.textView_explore);

        TextView xunjiren = (TextView) findViewById(R.id.textView_xunjiren);
        xunjiren.setOnClickListener(this);

        explore.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        int width = relativeLayout.getWidth()/3;

        int id = v.getId();

        switch (id){

            case R.id.textView_tab_guangchang:

                viewPager.setCurrentItem(0);
                setTabBiaoJi(0, 0, 0, 0);
                break;

            case R.id.textView_tab_huodong:

                viewPager.setCurrentItem(1);
                setTabBiaoJi(width, 0, 0, 0);
                break;

            case R.id.textView_tab_guanzhu:

                viewPager.setCurrentItem(2);
                setTabBiaoJi(width * 2, 0, 0, 0);
                break;

            case R.id.imageView_mine:

                drawerLayout.openDrawer(Gravity.RIGHT);
                break;

            case R.id.textView_explore:

                startActivity(new Intent(this, DiscoverActivity.class));
                break;

            case R.id.textView_xunjiren:

                startActivity(new Intent(this, PeopleActivity.class));
                break;

            case R.id.imgBtn_fubu:

                Intent intent = new Intent(this, FaBuActivity.class);
                startActivity(intent);
                break;
        }


    }

    private void setTabBiaoJi(int left, int top, int right, int bottom){

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewTabBiaoJi.getLayoutParams();

        params.setMargins(left, top, right, bottom);

        viewTabBiaoJi.setLayoutParams(params);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        int width = relativeLayout.getWidth()/3;

        int left = (int) (position * width + positionOffset * width);

        setTabBiaoJi(left, 0, 0, 0);

    }

    @Override
    public void onPageSelected(int position) {}

    @Override
    public void onPageScrollStateChanged(int state) {}

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode){


            case KeyEvent.KEYCODE_BACK:

                if(isExit){

                    return super.onKeyDown(keyCode, event);
                }else {

                    Toast.makeText(this, "再按一下退出程序", Toast.LENGTH_SHORT).show();
                    isExit = true;

                    new Thread(new Runnable() {

                        @Override
                        public void run() {

                            try {

                                Thread.sleep(2000);

                                isExit = false;

                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                        }

                    }).start();
                    return false;
                }

            default:
                return super.onKeyDown(keyCode, event);

        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
}
